package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.common.utils.tree.TreeBuilder;
import com.hjx.movie.common.utils.tree.TreeNode;
import com.hjx.movie.core.system.service.admin.IApiService;
import com.hjx.movie.core.system.service.admin.IRoleApiService;
import com.hjx.movie.core.system.vo.ApiTreeNode;
import com.hjx.movie.core.system.vo.CheckedIdsVO;
import com.hjx.movie.persistence.system.model.IvyApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/1/8 - 16:31
 */
@RestController
@Slf4j
@RequestMapping("/api")
public class ApiController {
    @Resource
    IApiService apiService;
    @Resource
    IRoleApiService roleApiService;


    @GetMapping("/tree")
    public Result findApiTree(@RequestParam(name = "rootApiId") Long rootApiId,
                              @RequestParam(name = "apiNameLike",required = false)String apiNameLike,
                              @RequestParam(name = "apiStatus",required = false)Boolean apiStatus){
        log.debug("findApiTree接受到的入参===> rootApiId:{},apiNameLike:{},apiStatus:{}",rootApiId,apiNameLike,apiStatus);
        if (StringUtils.isNotBlank(apiNameLike)||apiStatus!=null){
            List<ApiTreeNode> list =  apiService.selectApiTree(rootApiId,apiNameLike,apiStatus);
            list.sort(Comparator.comparing(TreeNode::getNodeId));
            return Result.success(list);
        }
        return Result.success(TreeBuilder.treeSort(apiService.selectApiTree(rootApiId,apiNameLike,apiStatus)));
    }

    @PostMapping("/add")
    public Result addApi(@RequestBody IvyApi ivyApi){
        log.debug("addApi接收到的入参===>:{}",ivyApi);
        if(apiService.addApi(ivyApi)){
            return Result.success("新增接口成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR);
    }

    @GetMapping("/delete")
    public Result deleteApi(@RequestParam("apiId")Long apiId){
        log.debug("deleteApi接收到的入参===>:{}",apiId);
        if (apiService.deleteApi(apiId)){
            return Result.success("删除接口成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR);
    }

    @PostMapping("/update")
    public Result updateApi(@RequestBody IvyApi api){
        log.debug("updateApi接收到的入参===>:{}",api);
        if (apiService.updateApi(api)){
            return Result.success("修改接口信息成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR);
    }


    @GetMapping("/findCheckedTree")
    public Result findApiCheckedKeys(@RequestParam("roleId") Long roleId){
        log.debug("接收到的入参===>:{}",roleId);
        Map<String,Object> map = new HashMap<>();
        map.put("tree",apiService.selectApiTree(1L,null,null));
        map.put("expandedKeys",apiService.findApiExpandedKeys());
        map.put("checkedKeys",apiService.findApiCheckedKeys(roleId));
        return Result.success(map);
    }

    @PostMapping("/addOrDeleteRoleApi")
    public Result addRoleMenu(@RequestBody CheckedIdsVO checkedIdsVO){
        if (roleApiService.addOrDeleteApiBatch(checkedIdsVO.getId(), checkedIdsVO.getCheckedIds())){
            return Result.success("保存接口权限成功!");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"保存接口权限失败!");
    }

}
